# -*- coding: utf-8 -*-
# @Time    : 2020/9/8 23:24
# @Author  : liupengfei
# @File    : serializers.py

from rest_framework import serializers
from apps.member.serializers import MemeberInfoAppSerializer
from utils.serializersfield import DelayTimeCharField
from .models import Comment
from ..models import MemberRecord


class CommentArticleAppSerializer(serializers.ModelSerializer):
    member = serializers.SerializerMethodField()
    delay_time = DelayTimeCharField(source='create_time', read_only=True)
    is_like = serializers.SerializerMethodField()

    def get_is_like(self, obj):
        user = self.context['request'].user
        if user.is_anonymous:
            return False
        return MemberRecord.is_like(obj, user.member)

    def get_member(self, obj):
        return MemeberInfoAppSerializer(obj.member).data

    # 文章评论
    class Meta:
        model = Comment
        fields = ('id', 'is_like', 'content', 'like_count', 'delay_time', 'member')


class CommentAppSerializer(serializers.ModelSerializer):
    # 个人
    class Meta:
        model = Comment
        fields = '__all__'
